//
//  GoodsModel.m
//  test
//
//  Created by fcn on 2020/8/14.
//  Copyright © 2020 hdj. All rights reserved.
//

#import "GoodsModel.h"

@implementation GoodsModel


+ (NSArray<GoodsModel*> *)readGoodsList{
    NSString* path = [[NSBundle mainBundle] pathForResource:goods_list_test ofType:@"xlsx"];
//    NSLog(@"%@", path);
    
    NSURL* url = [NSURL URLWithString:path];
    
    MYCExcelWookbook* wb = [[MYCExcelWookbook alloc] initWithExcelFilePathUrl:url];
    MYCExcelSheet* sheet = wb.sheetArray.firstObject;
    
    NSMutableArray* dataArr = [NSMutableArray new];

    NSInteger currentRow = 0;
    for (MYCExcelCell* cell in sheet.cellArray) {
        NSMutableArray<MYCExcelCell *>* arr = [NSMutableArray new];
        if (cell.row == currentRow) {
            arr = dataArr.lastObject;
            [arr addObject:cell];
        }else{
            currentRow = cell.row;
            [dataArr addObject:arr];
            [arr addObject:cell];
        }
    }
    NSArray* res = [self cells2models:dataArr];
    return res;
}

+ (NSArray<GoodsModel*>*)cells2models:(NSArray<NSMutableArray<MYCExcelCell*>*> *)dataArr{
    NSMutableArray<GoodsModel*> * modelArr = [NSMutableArray new];
    NSMutableArray<NSString*> * attriArr = [NSMutableArray new];

    for (NSInteger i = 0; i < dataArr.count; i++) {
        NSArray<MYCExcelCell *> *arr = dataArr[i];
        if (i == 0) {
            // 第一行是标题
            for (MYCExcelCell* cell in arr) {
                // 创建属性名映射数组
                NSString* attriName = [self getAttriName:cell.stringValue];
                if (attriName) {
                    [attriArr addObject:attriName];
                }
            }
            if (attriArr.count != dataArr.firstObject.count) {
                NSLog(@"属性数量错误");
                return nil;
            }
        }else{
            GoodsModel* model = [GoodsModel new];
            for (NSInteger j = 0; j < arr.count; j++) {
                MYCExcelCell *cell = arr[j];
                NSString* attriName = attriArr[j];
                if (cell.stringValue) {
                    [model setValue:cell.stringValue forKey:attriName];
                }
            }
            [modelArr addObject:model];
        }
    }
    
    return modelArr;
}

+ (NSString*)getAttriName:(NSString*)name{
    if ([name isEqualToString:@"商品ID"]) {
        return @"goodID";
    }else if ([name isEqualToString:@"品名"]){
        return @"goodName";
    }else if ([name isEqualToString:@"关键词"]){
        return @"keyWord";
    }else if ([name isEqualToString:@"规格（克）"]){
        return @"specifications";
    }else if ([name isEqualToString:@"出厂价"]){
        return @"exFactoryPrice";
    }else if ([name isEqualToString:@"采购价"]){
        return @"purchasePrice";
    }else if ([name isEqualToString:@"一口价"]){
        return @"retailPrice";
    }else if ([name isEqualToString:@"实际售价"]){
        return @"actualPrice";
    }else if ([name isEqualToString:@"袋数"]){
        return @"quantity";
    }
    return nil;
}

@end
